package com.ruoyi.tb.lmt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.tb.lmt.domain.bo.DrMonoVsetBo;
import com.ruoyi.tb.lmt.domain.vo.DrMonoVsetVo;
import com.ruoyi.tb.lmt.domain.DrMonoVset;
import com.ruoyi.tb.lmt.mapper.DrMonoVsetMapper;
import com.ruoyi.tb.lmt.service.IDrMonoVsetService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 油压减震器Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-13
 */
@RequiredArgsConstructor
@Service
public class DrMonoVsetServiceImpl implements IDrMonoVsetService {

    private final DrMonoVsetMapper baseMapper;

    /**
     * 查询油压减震器
     */
    @Override
    public DrMonoVsetVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询油压减震器列表
     */
    @Override
    public TableDataInfo<DrMonoVsetVo> queryPageList(DrMonoVsetBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DrMonoVset> lqw = buildQueryWrapper(bo);
        Page<DrMonoVsetVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询油压减震器列表
     */
    @Override
    public List<DrMonoVsetVo> queryList(DrMonoVsetBo bo) {
        LambdaQueryWrapper<DrMonoVset> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DrMonoVset> buildQueryWrapper(DrMonoVsetBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DrMonoVset> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, DrMonoVset::getId, bo.getId());
        lqw.eq(StringUtils.isNotBlank(bo.getDjNo()), DrMonoVset::getDjNo, bo.getDjNo());
        lqw.eq(bo.getDrId() != null, DrMonoVset::getDrId, bo.getDrId());
        lqw.eq(StringUtils.isNotBlank(bo.getDrDamperType()), DrMonoVset::getDrDamperType, bo.getDrDamperType());
        lqw.eq(StringUtils.isNotBlank(bo.getDrVelocitySet()), DrMonoVset::getDrVelocitySet, bo.getDrVelocitySet());
        lqw.eq(StringUtils.isNotBlank(bo.getDrForceTensionSet()), DrMonoVset::getDrForceTensionSet, bo.getDrForceTensionSet());
        lqw.eq(StringUtils.isNotBlank(bo.getDrForcePressSet()), DrMonoVset::getDrForcePressSet, bo.getDrForcePressSet());
        lqw.eq(StringUtils.isNotBlank(bo.getDrErrPset()), DrMonoVset::getDrErrPset, bo.getDrErrPset());
        lqw.eq(StringUtils.isNotBlank(bo.getDrErrNset()), DrMonoVset::getDrErrNset, bo.getDrErrNset());
        lqw.eq(StringUtils.isNotBlank(bo.getDrDissymmetrySet()), DrMonoVset::getDrDissymmetrySet, bo.getDrDissymmetrySet());
        lqw.eq(StringUtils.isNotBlank(bo.getDelFlag()), DrMonoVset::getDelFlag, bo.getDelFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), DrMonoVset::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getCreateTime() != null, DrMonoVset::getCreateTime, bo.getCreateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), DrMonoVset::getUpdateBy, bo.getUpdateBy());
        lqw.eq(bo.getUpdateTime() != null, DrMonoVset::getUpdateTime, bo.getUpdateTime());
        return lqw;
    }

    /**
     * 新增油压减震器
     */
    @Override
    public Boolean insertByBo(DrMonoVsetBo bo) {
        DrMonoVset add = BeanUtil.toBean(bo, DrMonoVset.class);
        add.setDelFlag("0");
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改油压减震器
     */
    @Override
    public Boolean updateByBo(DrMonoVsetBo bo) {
        DrMonoVset update = BeanUtil.toBean(bo, DrMonoVset.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(DrMonoVset entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除油压减震器
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Long getMaxDrID() {

        return baseMapper.getMaxDrID();
    }
}
